package com.wn.www.common.result;

import com.wn.www.common.result.enums.IErrorCode;
import com.wn.www.common.result.enums.impl.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@ApiModel("统一返回结果")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    @ApiModelProperty("响应编码")
    private int code;
    @ApiModelProperty("响应信息")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;

    private static <T> Result<T> common(int code,String message,T data){
        return new Result<>(code,message,data);
    }

    public static <T> Result<T> ok(){
        return common(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),null);
    }

    public static <T> Result<T> ok(T data){
        return common(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> Result<T> fail(){
        return common(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),null);
    }

    public static <T> Result<T> fail(String message){
        return common(ResultCode.FAILED.getCode(),message,null);
    }

    public static <T> Result<T> fail(T data){
        return common(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),data);
    }

    public static <T> Result<T> fail(String message,T data){
        return common(ResultCode.FAILED.getCode(),message,data);
    }

    public static <T> Result<T> fail(IErrorCode errorCode){
        return common(errorCode.getCode(), errorCode.getMessage(),null);
    }

    public static <T> Result<T> fail(int code,String message){
        return common(code, message,null);
    }

    public static <T> Result<T> fail(IErrorCode errorCode,T data){
        return common(errorCode.getCode(), errorCode.getMessage(),data);
    }


}
